package com.hks360.car_treasure.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TabHost;
import android.widget.TextView;

import com.hks360.car_treasure.R;
import com.hks360.car_treasure.activity.ConnectEquipmentActivity;
import com.hks360.car_treasure.activity.EfenceActivity;
import com.hks360.car_treasure.activity.MainActivity;
import com.hks360.car_treasure.activity.TrailActivity;
import com.hks360.car_treasure.common.AppConfig;
import com.hks360.car_treasure.common.Method;
import com.hks360.car_treasure.common.PrefKey;
import com.hks360.library.util.CommonUtil;
import com.hks360.library.util.PrefUtil;
import com.hks360.library.util.UIUtil;
import com.hks360.library.widget.MyGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by rwq on 2016/5/26.
 */
public class MainFragment extends BaseFragment {
    private MyGridView mGv;
    private String[] mTextArray;
    private BaseAdapter GvAdapter;
    private int[] picture = new int[]{
            R.drawable.item1, R.drawable.item2, R.drawable.item3,
            R.drawable.item4, R.drawable.item5, R.drawable.item6};

    private ViewPager mVp;
    private PagerAdapter VpAdapter;
    private int[] picture_vp = new int[]{
            R.mipmap.car1, R.mipmap.car2, R.mipmap.car3, R.mipmap.car4};
    private List<View> mListViews;

    private EfenceActivity efenceActivity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_main, null);
        initView();
        return mRootView;

    }

    @Override
    public void initView() {
        initTitleBar("首页");
        setupView();
        initData();
        addListener();


    }

    @Override
    public void viewClick(View v) {

    }


    private void setServer() {
        if(!Method.getServer().isEmpty()){
            AppConfig.mComManager.setADDRESS(Method.getServer().split(":")[0].trim());
            AppConfig.mComManager.setPOT(Integer.valueOf(Method.getServer().split(":")[1].trim()));
            Log.e("RWQ111", Method.getServer().split(":")[0]);
            Log.e("RWQ111",Method.getServer().split(":")[1]);
        }
    }

    @Override
    public void setupView() {
        mListViews = new ArrayList<>();
        mGv = UIUtil.findViewById(mRootView, R.id.main_gv);
        mVp = UIUtil.findViewById(mRootView, R.id.main_vp);
        mTextArray = getResources().getStringArray(R.array.gvcontent);
        GvAdapter = new BaseAdapter() {

            @Override
            public int getCount() {
                return mTextArray.length;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;
                if (convertView == null) {
                    convertView = View.inflate(getContext(), R.layout.gv_item, null);
                    holder = new ViewHolder();
                    holder.text = (TextView) convertView.findViewById(R.id.gv_item_tv);
                    holder.icon = (ImageView) convertView.findViewById(R.id.gv_item_iv);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                holder.text.setText(mTextArray[position]);
                if(EfenceActivity.isAlert && position == 2){
                    holder.icon.setImageResource(R.drawable.alert_fence);
                }else {
                    holder.icon.setImageResource(picture[position]);
                }
                return convertView;
            }
        };
        VpAdapter = new PagerAdapter() {
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mListViews.get(position));//删除页卡
            }


            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(mListViews.get(position));
                return mListViews.get(position);
            }

            @Override
            public int getCount() {
                return picture_vp.length;
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == arg1;//官方提示这样写
            }

        };
    }

    @Override
    public void initData() {
        if(!Method.getTboxId().equals(PrefUtil.getString(getContext(), PrefKey.TBOX_ID,""))){
            setServer();
            AppConfig.mComManager.loading(Method.getTboxId(), PrefUtil.getString(getContext(), PrefKey.LOGIN_PHONE, ""), PrefUtil.getString(getContext(), PrefKey.LOGIN_PASSWORD, ""));
            PrefUtil.putString(getContext(),PrefKey.TBOX_ID,Method.getTboxId());
        }

        for (int i = 0; i < picture_vp.length; i++) {
            View view;
            view = View.inflate(getContext(), R.layout.main_vp, null);
            ImageView imageView = UIUtil.findViewById(view, R.id.main_vp_iv);
            imageView.setImageResource(picture_vp[i]);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    CommonUtil.showToast(getContext(), "我被点击了");
                }
            });
            mListViews.add(view);
        }

    }

    static class ViewHolder {
        TextView text;
        ImageView icon;
    }

    @Override
    public void onResume() {
        super.onResume();
        if(AppConfig.user.getTboxinfo().size() == 0){
            EfenceActivity.isAlert = false;
        }
        if(EfenceActivity.isAlert){
            GvAdapter.notifyDataSetChanged();
        }
    }
    @Override
    public void addListener() {
        mGv.setAdapter(GvAdapter);
        mVp.setAdapter(VpAdapter);

        mGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (mTextArray[position]) {
                    case "爱车位置":
                        TabHost mTable = ((MainActivity) getActivity()).getTabHost();
                        mTable.setCurrentTab(2);
                        break;
                    case "电子围栏":
                        if(AppConfig.user.getTboxinfo().size() == 0){
                            CommonUtil.showToast(getContext(),"当前未绑定任何设备");
                            CommonUtil.openActivity(getContext(), ConnectEquipmentActivity.class);
                        }else{
                            CommonUtil.openActivity(getContext(), EfenceActivity.class);
                        }
                        break;
                    case "爱车轨迹":
                        CommonUtil.openActivity(getContext(), TrailActivity.class);
                        break;
                }
            }
        });
    }


}
